package com.offcn.sellergoods.controller;

import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.sellergoods.entity.SpecEntity;
import com.offcn.sellergoods.service.SpecEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/spec")
public class SpecEntityController {

    @Autowired
    private SpecEntityService specEntityService;

    @PostMapping("/")
    public Result add(@RequestBody SpecEntity specEntity){
        try {
            specEntityService.add(specEntity);
            return new Result(true, StatusCode.OK,"新增规格成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"新增规格失败");
        }
    }

    @GetMapping("/{id}")
    public Result<SpecEntity> findById(@PathVariable Long id){
        SpecEntity specEntity = specEntityService.findById(id);
        return new Result<>(true,StatusCode.OK,specEntity,"查询规格成功");
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody SpecEntity specEntity,@PathVariable Long id){
        try {
            specEntity.getSpecification().setId(id);
            specEntityService.update(specEntity);
            return new Result(true,StatusCode.OK,"修改规格成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改规格失败");
        }


    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        try {
            specEntityService.delete(id);
            return new Result(true,StatusCode.OK,"删除规格成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"删除规格失败");
        }
    }

    @GetMapping("/selectOptionSpec")
    public Result<List<Map>> selectOptionSpec(){
        List<Map> maps = specEntityService.selectOptionSepc();
        return new Result<>(true,StatusCode.OK,maps,"查询规格下拉选项成功");
    }
}
